﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using ProjectManagement;
using System.IO;

namespace GeneratorManager
{
    public partial class FrmLoadProject : DevExpress.XtraEditors.XtraForm
    {
        public FrmLoadProject()
        {
            InitializeComponent();
        }

        private void FrmLoadProjectLoad(object sender, EventArgs e)
        {
            try
            {
                FillGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error al leer la lista de proyectos. {0}", ex.Message), 
                                "Error no controlado.", 
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        private void FillGrid()
        {
            PMHelper.LoadProjects();
            grcProjects.DataSource = PMHelper.ColProjects.ColProjects;
        }

        private void grvProjects_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.Name == "itemPath")
            {
                if (!Directory.Exists(e.CellValue.ToString()))
                {
                    e.Appearance.ForeColor = Color.Red;
                }
            }
        }

        private void BtnUploadClick(object sender, EventArgs e)
        {
            try
            {
                FillGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error al leer la lista de proyectos. {0}", ex.Message),
                                "Error no controlado.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        private void BtnCargarClick(object sender, EventArgs e)
        {
            try
            {
                if (grvProjects.SelectedRowsCount == 0)
                {
                    throw new Exception("No se ha seleccionado ningún proyecto.");
                }

                var currentRowHandle = grvProjects.GetSelectedRows()[0];
                var project = (Project)grvProjects.GetRow(currentRowHandle);

                if (!Directory.Exists(project.Path))
                {
                    throw new Exception(string.Format("No se puede cargar el proyecto {0} porque la ruta no existe el directorio.", project.Name));
                }

                PMHelper.CurrentProject = project;

                if(File.Exists(string.Format("{0}\\databases.xml", PMHelper.CurrentProject.Path)))
                    DatabaseManagement.DBManager.LoadDatabases(PMHelper.CurrentProject.Path);

                MessageBox.Show(string.Format("Se cargo correctamente el projecto: {0}", project.Name),
                                "Projecto Cargado.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information
                    );
            }
        }

        private void grvProjects_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            btnCargar.Enabled = true;
        }
    }
}